//
//  LogOffViewController.m
//  BDD_TWO
//
//  Created by XTW on 2023/5/29.
//

#import "LogOffViewController.h"
#import "MIPhotoBrowser.h"
#import "UIColor+Hexadecimal.h"
#import "CustomAlertView.h"
#import "TabBarView.h"
#import <WFChatClient/WFCChatClient.h>
#import "AppService.h"

@interface LogOffViewController ()<MIPhotoBrowserDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *codeImageView;
@property (weak, nonatomic) IBOutlet UILabel *dot1;
@property (weak, nonatomic) IBOutlet UILabel *dot2;
@property (weak, nonatomic) IBOutlet UILabel *dot3;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

@implementation LogOffViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注销账户";
    [self addBackBtn];
    [self.scrollView setFrame:CGRectMake(0, IS_iPhoneX_Top + NavBarHeight, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - IS_iPhoneX_Top - IS_iPhoneX_Bottom - NavBarHeight - 60)];
    self.scrollView.alwaysBounceVertical = YES;
    [self.scrollView setContentSize:CGSizeMake(0, 568)];
    
    [self.sureBtn setFrame:CGRectMake(15, SCREEN_HEIGHT_DEVICE - IS_iPhoneX_Bottom - 10 - 44, SCREEN_WIDTH_DEVICE - 30, 44)];
    [self.sureBtn setBackgroundImage:getColorToImage(TextGrayColor) forState:UIControlStateNormal];
    [self.view bringSubviewToFront:self.scrollView];
        
    UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(stepTapAction:)];
    tapRecognize.numberOfTapsRequired = 1;
    [self.codeImageView addGestureRecognizer:tapRecognize];
    // Do any additional setup after loading the view from its nib.
}



- (IBAction)surePress {
    CustomAlertView *alertView = [[CustomAlertView alloc] initItem:@"是否要注销当前账号？" withButtons:[NSArray arrayWithObjects:@"取消", @"确定", nil]];
    alertView.SureHandle = ^{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"savedName"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"savedToken"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"savedUserId"];
        [[AppService sharedAppService] clearAppServiceAuthInfos];
        [[NSUserDefaults standardUserDefaults] synchronize];
        //退出后就不需要推送了，第一个参数为YES
        //如果希望再次登录时能够保留历史记录，第二个参数为NO。如果需要清除掉本地历史记录第二个参数用YES
        [[WFCCNetworkService sharedInstance] disconnect:YES clearSession:NO];
        
        [UserManger logoutCurrentUser:nil];
        [self.navigationController popToRootViewControllerAnimated:NO];
        [[TabBarView sharedTabBarView] actionLogin];
    };
    [alertView show];
}


- (void)stepTapAction:(UIGestureRecognizer *)gesture{
    MIPhotoBrowser *photoBrowser = [[MIPhotoBrowser alloc] init];
    photoBrowser.delegate = self;
    photoBrowser.sourceImagesContainerView = self.codeImageView.superview;
    photoBrowser.currentImgaeView = self.codeImageView;
    photoBrowser.imageCount = 1;
    photoBrowser.currentImageIndex = 0;
    [photoBrowser show];
}

- (UIImage *)photoBrowser:(MIPhotoBrowser *)photoBrowser placeholderImageForIndex:(NSInteger)index{
    return [self getImageFromView:self.codeImageView];
}

- (void)photoBrowserColse {
}

- (UIImage *)getImageFromView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
@end
